"""
Contains classes and functions pertaining to buildings.
"""
import os, pygame
from qnd import *

####CONSTANTS####
#Calculating file paths
BASEPATH = os.path.abspath(__file__)
BASEPATH = BASEPATH[:BASEPATH.index('src')]

#Images to use for buildings
SPRITES = [
	load_image('building1.png'),
	load_image('building2.png')
]

class Building:
	"""
	A building. Initialize it with a tile position and an image. Size
	will be calculated based on the image, rounding down to the nearest
	tile width and height.
	"""
	def __init__(self,(x,y),image_number=0,tileheight=32,tilewidth=32):
		self.sprite = SPRITES[image_number]
		self.position = self.x,self.y = x,y
		self.size = self.width,self.height = self.sprite.get_width()/32,self.sprite.get_height()/32
	def draw(self,screen,camera,tilewidth=32,tileheight=32):
		"""Draw the building on the screen"""
		draw_image(screen,self.sprite,(self.x*tilewidth,self.y*tilewidth),camera=camera)
		#debug drawing
		#~ drawx = self.x*tilewidth-camera.x
		#~ drawy = self.y*tileheight-camera.y
		#~ draw_width = self.width*tilewidth
		#~ draw_height = self.height*tileheight
		#~ pygame.draw.rect(screen,(0,0,0),(drawx,drawy,draw_width,draw_height),3)
		
def load_buildings(filename,folder='levels'):
	"""Returns the list of buildings from the file"""

	filename += '.buildings'
	print "Loading Buildings", filename
	
	file = open(os.path.join(BASEPATH,folder,filename),'r')
	lines = file.readlines()
	file.close()

	buildings = []
	for line in lines:
		pos,image_number = line.split()
		pos = map(int,pos.split(','))
		building = Building(pos,int(image_number))
		buildings.append(building)
	return buildings


